package com.google.zxing.client.android;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import up_down_share.updaonwshare.v01.R;

import com.integration.IntentResult;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
	
	  private final Map<String,Object> moreExtras = new HashMap<String,Object>(3);
	  public static final int REQUEST_CODE = 0x0000c0de; // Only use bottom 16 bits
	  public static final Collection<String> QR_CODE_TYPES = Collections.singleton("QR_CODE");

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Button button = (Button)findViewById(R.id.button1);
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			startIntent(MainActivity.this,MainActivity.QR_CODE_TYPES);
				
			}
		});
		
	}
	
	@Override
	public void onActivityResult(int requestCode,int resultCode, Intent intent){
		
		IntentResult result = parseActivityResult(requestCode, resultCode, intent);
		
		if(result != null){
			
			String contents = result.getContents();
			if(contents != null)
			{
				new AlertDialog.Builder(MainActivity.this).setMessage("The Scan result :" + contents)
				.setNegativeButton("Quit", null)
				.create()
				.show();
				
			}else{
				
				new AlertDialog.Builder(MainActivity.this).setMessage("The Scan result : null")
				.setNegativeButton("Quit", null)
				.create()
				.show();
			}
		}
		
	}



	
	public void startIntent(Activity activity,Collection<String> desiredBarcodeFormats){
		  
		//"com.google.zxing.client.android.SCAN"
		 Intent intentScan = new Intent("com.google.zxing.client.android.SCAN");
		    intentScan.addCategory(Intent.CATEGORY_DEFAULT);
		   // ComponentName com = new ComponentName("com.google.zxing.client.android","CaptureActivity");
		    //com.google.zxing.client.android.CaptureActivity
		   // intentScan.setComponent(com);
		    

		    // check which types of codes to scan for
		    if (desiredBarcodeFormats != null) {
		      // set the desired barcode types
		      StringBuilder joinedByComma = new StringBuilder();
		      for (String format : desiredBarcodeFormats) {
		        if (joinedByComma.length() > 0) {
		          joinedByComma.append(',');
		        }
		        joinedByComma.append(format);
		      }
		      intentScan.putExtra("SCAN_FORMATS", joinedByComma.toString());
		    }
		    
		    //intentScan.setPackage(targetAppPackage);
		    intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		    intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
		    attachMoreExtras(intentScan);
		    activity.startActivityForResult(intentScan, REQUEST_CODE);
		   // activity.startActivity(intentScan);
		    
	}
	

	  private void attachMoreExtras(Intent intent) {
	    for (Map.Entry<String,Object> entry : moreExtras.entrySet()) {
	      String key = entry.getKey();
	      Object value = entry.getValue();
	      // Kind of hacky
	      if (value instanceof Integer) {
	        intent.putExtra(key, (Integer) value);
	      } else if (value instanceof Long) {
	        intent.putExtra(key, (Long) value);
	      } else if (value instanceof Boolean) {
	        intent.putExtra(key, (Boolean) value);
	      } else if (value instanceof Double) {
	        intent.putExtra(key, (Double) value);
	      } else if (value instanceof Float) {
	        intent.putExtra(key, (Float) value);
	      } else if (value instanceof Bundle) {
	        intent.putExtra(key, (Bundle) value);
	      } else {
	        intent.putExtra(key, value.toString());
	      }
	    }
	  }
	  
	  public static IntentResult parseActivityResult(int requestCode, int resultCode, Intent intent) {
		    if (requestCode == REQUEST_CODE) {
		      if (resultCode == Activity.RESULT_OK) {
		        String contents = intent.getStringExtra("SCAN_RESULT");
		        String formatName = intent.getStringExtra("SCAN_RESULT_FORMAT");
		        byte[] rawBytes = intent.getByteArrayExtra("SCAN_RESULT_BYTES");
		        int intentOrientation = intent.getIntExtra("SCAN_RESULT_ORIENTATION", Integer.MIN_VALUE);
		        Integer orientation = intentOrientation == Integer.MIN_VALUE ? null : intentOrientation;
		        String errorCorrectionLevel = intent.getStringExtra("SCAN_RESULT_ERROR_CORRECTION_LEVEL");
		        return new IntentResult(contents,
		                                formatName,
		                                rawBytes,
		                                orientation,
		                                errorCorrectionLevel);
		      }
		      return new IntentResult();
		    }
		    return null;
		  }
}
